package com.cowin.config;

import com.cowin.base.commom.ProjectConstant;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * Copyright (C), 1998-2018, 奇瑞信息
 *
 * @author : 于超
 * @Date: 2019/8/30 10:10
 * @Description:
 */
@Configuration
public class MybatisConfigurer {
    @Bean
    public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setTypeAliasesPackage("com.cowin.sys.model;com.cowin.inf.model");
        /*
         * 配置分页插件，详情请查阅官方文档
         */
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        /*
         * 分页尺寸为0时查询所有纪录不再执行分页
         */
        properties.setProperty("pageSizeZero", "true");
        /*
         * 页码<=0 查询第一页，页码>=总页数查询最后一页
         */
        properties.setProperty("reasonable", "true");
        /*
         * 支持通过 Mapper 接口参数来传递分页参数
         */
        properties.setProperty("supportMethodsArguments", "true");
        pageHelper.setProperties(properties);
        /*
         * 添加插件
         */
        factory.setPlugins(new Interceptor[]{pageHelper});
        /*
         * 添加XML目录
         */
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        factory.setMapperLocations(resolver.getResources("classpath:mapper/**/*.xml"));
        factory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        return factory.getObject();
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
        mapperScannerConfigurer.setBasePackage("com.cowin.sys.dao;com.cowin.inf.dao");
        /*
         * 配置通用Mapper，详情请查阅官方文档
         */
        Properties properties = new Properties();
        properties.setProperty("mappers", ProjectConstant.MAPPER_INTERFACE_REFERENCE);
        /*
         * insert、update是否判断字符串类型!='' 即 test="str != null"表达式内是否追加 and str != ''
         */
        properties.setProperty("notEmpty", "false");
        /*
         *主键UUID回写方法执行顺序,默认AFTER,可选值为(BEFORE|AFTER)
         */
        properties.setProperty("ORDER", "BEFORE");
        properties.setProperty("IDENTITY", "Oracle");
        mapperScannerConfigurer.setProperties(properties);
        return mapperScannerConfigurer;
    }
}
